
import { _decorator, Component, Node, Vec3, SkinnedMeshRenderer, Material, renderer, Vec4, Texture2D, MeshRenderer } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';
import Loader from '../../Script/Common/Loader';
import { EventType } from '../../Script/GameSpecial/GameEventType';
import Action3dManager, { ActionMngType } from '../../Script/Common/Action3dManager';
const { ccclass, property } = _decorator;

@ccclass('RoleDeadAnim2')
export class RoleDeadAnim2 extends yyComponent {
    public init() {
        this.initBodies();
        this.initSkin();
    }
    public reset() {
        this.resetBodies();
        this.resetSkin();
    }

    @property(Node)
    protected bodyLayer: Node = null;
    protected transf: { [node: string]: { node: Node, p: Vec3, s: Vec3, e: Vec3 } } = {};
    //正在播放动画的躯体数量
    protected bodyAnimCount: number = 0;
    protected initBodies() {
        this.transf = {};
        let children = this.bodyLayer.children;
        for (let i = children.length - 1; i >= 0; --i) {
            let node = children[i];
            this.transf[node.name] = {
                node: node,
                p: node.getPosition(),
                s: node.scale,
                e: node.eulerAngles,
            };
        }
        this.bodyAnimCount = 0;
    }
    protected resetBodies() {
        let actMng = Action3dManager.getMng(ActionMngType.Level);
        for (let key in this.transf) {
            let data = this.transf[key];
            data.node.setPosition(data.p);
            data.node.setScale(data.s);
            data.node.eulerAngles = data.e;
            actMng.stopAllActions(data.node);
        }
        this.bodyAnimCount = 0;
    }
    protected playBodyAnim() {
        let rate = 1 / 1;
        let x = 3 * rate;
        let y = 0 * rate;
        let z = 3 * rate;
        let g0 = 20 * rate;
        let g1 = 10 * rate;
        let d = 1;
        let actMng = Action3dManager.getMng(ActionMngType.Level);
        for (let key in this.transf) {
            let node = this.transf[key].node;
            let jump = Action3dManager.jumpTo(d, x * (Math.random() - 0.5), y * (Math.random() - 0.5), z * (Math.random() - 0.5), Math.random() * g1 + g0);
            let rot = Action3dManager.rotateTo(d, (Math.random() - 0.5) * 360, (Math.random() - 0.5) * 360, (Math.random() - 0.5) * 360);
            let spawn = Action3dManager.spawn(jump, rot);
            let cb = Action3dManager.callFun(this.onBodyAnimFinish, this);
            let seq = Action3dManager.sequence(spawn, cb);
            actMng.runAction(node, seq);
            this.bodyAnimCount++;
        }
    }
    protected onBodyAnimFinish() {
        this.bodyAnimCount -= 1;
        if (this.bodyAnimCount <= 0) {
            this.onAnimFinish();
        }
    }
    protected onAnimFinish() {
        if (this.isPlayer) {
            this.emit(EventType.LevelEvent.playerDeadAnimFinished);
        }
    }

    public play(data: { p: Vec3, radian: number, scale: number, isPlayer?: boolean, skin: number }) {
        let s = data.scale;
        this.node.setScale(s, s, s);
        this.node.setPosition(data.p);
        this.node.setRotation(0, Math.sin(data.radian * 0.5), 0, Math.cos(data.radian * 0.5));
        this.isPlayer = !!data.isPlayer;
        this.setSkin(data.skin);
        this.playBodyAnim();
    }

    protected isPlayer: boolean = false;

    protected mats: Material[] = [];
    protected beHitTime: number = 0;
    protected initSkin() {
        let children = this.bodyLayer.children;
        for (let i = children.length - 1; i >= 0; --i) {
            let skin = children[i].children[0].getComponent(MeshRenderer);
            this.mats.push(skin.getMaterialInstance(0));
        }
        this.beHitTime = 0;
    }
    protected resetSkin() {
    }
    protected setSkin(skinId) {
        //加载对应贴图赋值给材质球
        Loader.loadBundleRes("Role", "PlayerSkin2/skin" + skinId, (res) => {
            let t = new Texture2D();
            t.image = res;
            for (let i = this.mats.length - 1; i >= 0; --i) {
                this.mats[i].setProperty("mainTexture", t);
            }
        });
    }
    protected setMatBeHitColor() {
        this.beHitTime = 0.5;
    }
    public customUpdate(dt: number) {
        if (this.beHitTime <= 0) return;
        this.beHitTime -= dt;
        if (this.beHitTime <= 0) {
            this.beHitTime = 0;
            this.emit(EventType.LevelEvent.deadAnimFinish, this);
        }
    }

}
